<?php

namespace app\index\controller;

use app\admin\model\GiftList;
use app\admin\model\Goods;
use app\admin\model\Locks;
use app\admin\model\MiningCoin;
use app\admin\model\MiningUserWallet;
use app\admin\model\UserMiner;
use app\admin\model\WaterLock;
use app\admin\model\WaterMining;
use app\common\controller\Frontend;
use app\common\model\Config;
use app\common\model\User;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function test()
    {
        exit;
//        $user_list = UserMiner::where('id','>=',"128")->where('id','<=',143)->select();
//        foreach ($user_list as $value) {
//
////            WaterMining::where('user_id',$value->user_id)->where('type',10)->delete();
//            $count = Locks::where('user_id', $value->user_id)->count();
//            if ($count <=2) {
//                User::where('id',$value->user_id)->update(['locks'=>0]);
//                WaterLock::where('user_id',$value->user_id)->delete();
////                Locks::where('user_id',$value->user_id)->delete();
//            }
//        }
//        $userlist = User::select();
//
//        $gift_day = Config::where('name','gift_day')->value('value');
//        $gift_limit = Config::where('name','gift_limit')->value('value');
//        foreach ($userlist  as $value) {
//            GiftList::create([
//                'detail_id' => $value->id,
//                'user_id' => $value->pid,
//                'number' =>$gift_limit,
//                'balance' => $gift_limit,
//                'end_time' => date('Y-m-d',strtotime("+$gift_day day"))
//            ]);
//            GiftList::create([
//                'user_id' => $value->id,
//                'number' =>$gift_limit,
//                'balance' => $gift_limit,
//                'end_time' => date('Y-m-d',strtotime("+$gift_day day"))
//            ]);
//        }
//        $data = User::select();
//        $MiningType = MiningCoin::select();
//
//        foreach ($data as $val) {
//            foreach ($MiningType as $value) {
//                $exist = MiningUserWallet::where('user_id',$val->id)->where('mining_coin_id',$value->id)->find();
//                if (empty($exist)) {
//                    MiningUserWallet::create([
//                        'user_id' => $val->id,
//                        'mining_coin_id' => $value->id,
//                        'mining_coin_str' => $value->coin_type
//                    ]);
//                }
//
//            }
//        }
        exit;

        $user = User::select();
        $coin_list = ['USDT','FIL'];
        foreach ($user as $value)
        {
            foreach ($coin_list as $key => $val) {
                $balance = WaterMining::where('user_id',$value->id)->where('coin_type',$val)->order('id','desc')->value('balance');
                if ($balance === null) {
                    continue;
                }
                $exist = MiningUserWallet::where('user_id',$value->id)->where('mining_coin_str',$val)->find();
                if (empty($exist)) {

                    MiningUserWallet::create([
                        'user_id' => $value->id,
                        'balance' => $balance,
                        'mining_coin_id' => $key+1,
                        'mining_coin_str' => $val,
                        'address' => '',
                    ]);
                } else {
                    MiningUserWallet::where('user_id',$value->id)->where('mining_coin_str',$val)->update(['balance' => $balance,'mining_coin_id' => $key+1,'mining_coin_str' => $val]);

                }
            }
        }
            exit;
//        $data = Locks::whereTime('create_time','today')->select();
//
//        foreach ($data as $value)
//        {
//            $user_id = $value->user_id;
//
////            $balance = $value->balance / 10;
////            $money = $value->money / 10;
////            $day_release = $value->day_release / 10;
////            Locks::where('id',$value->id)->update([
////                'day_release' => $day_release,
////                'balance' => $balance,
////                'money' => $money
////            ]);
////            $dec_locks = $value->money - $money;
////            $locks = User::where('id',$value->user_id)->value('locks');
////            \app\common\model\User::where('id',$value->user_id)->setDec("locks",$dec_locks);
////            WaterLock::create([
////                'user_id' => $value->user_id,
////                'money' => "-".$dec_locks,
////                'balance' => $locks - $dec_locks,
////                'type' => 0,
////            ]);
//
//            $profit = $value->day_release * 9;
//            $balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
//                ->value('balance');
//            MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
//                ->setDec('balance',abs($profit));
//            WaterMining::create([
////                        'detail_id' => $user_miner->id,
//                'user_id' => $user_id,
//                'type' => MINING_WATER_REDUCE,
//                'coin_type' => COIN2,
//                'money' => "-".$profit,
//                'balance' => $balance - $profit
//            ]);
//        }
//        $data = WaterMining::where('type',15)->whereTime('create_time','today')->select();
//        $user_list = [];
//        foreach ($data as $value)
//        {
//            if (empty($user_list[$value->user_id])) {
//                $user_list[$value->user_id] = 0;
//            }
//            $money = round($value->money / 10,6);
//            $user_list[$value->user_id] = $user_list[$value->user_id] + $money - $value->money;
//        }
//        $data = WaterMining::where('type',9)->whereTime('create_time','today')->select();
//        foreach ($data as $value)
//        {
//            if (empty($user_list[$value->user_id])) {
//                $user_list[$value->user_id] = 0;
//            }
//            $money = round($value->money / 10,6);
//            $user_list[$value->user_id] = $user_list[$value->user_id] + $money - $value->money;
//        }
//
//        foreach ($user_list as $user_id => $profit) {
//            $balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
//                ->value('balance');
//            MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
//                ->setDec('balance',abs($profit));
//            WaterMining::create([
////                        'detail_id' => $user_miner->id,
//                'user_id' => $user_id,
//                'type' => MINING_WATER_WATER_BACKEND,
//                'coin_type' => COIN2,
//                'money' => $profit,
//                'balance' => $balance + $profit
//            ]);
//        }
        exit;
//        bcadd(123.1234, 0, 3);
        $a = 1.5;
        $a = rounddown($a,0);
//        $new_num=floor($a*1000)/1000;
        print_r($a);exit;
        exit;
        $data = Db::name('import_data')->select();
        $t_fil = Config::where('name','t_fil')->value('value');
        $t_gas = Config::where('name','t_gas')->value('value');
        foreach ($data as $value)
        {
            $user = \app\common\model\User::where('mobile',$value['mobile'])->find();
            if (empty($user)) {
                var_dump($value['mobile']);
                continue;
            }
            $user_id = $user->id;
            if (!empty($value['balance'])) {
                $balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)->value('balance');
                \app\admin\model\WaterMining::create([
//                    'detail_id' => $order_data->id,
                    'user_id' => $user_id,
                    'type' => MINING_WATER_MINING,
                    'coin_type' => "FIL",
                    'money' =>  $value['balance'],
                    'balance' => $balance + $value['balance']
                ]);
                MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)->setInc('balance',$value['balance']);
            }


            $goods_data = Goods::where('id',1)->find();
            $number = $value['weijihuo'] + $value['jihuo'];
            UserMiner::create([
                'order_no' => generate_order_number(),
                'name' => $goods_data->name,
                'user_id' =>$user_id,
                'image' => $goods_data->image,
                'number' => $number,
                'balance_number' => $value['jihuo'],
                'max_pledge' => $t_fil * $number,
                'pledge' => $value['pledge'],
                'gift_pledge' => 0,
                'gas' => $value['gas'],
                'max_gas' => $t_gas * $number,
                'price' => $goods_data->price,
                'money' => $goods_data->price * $number,
                'begin_day' => $goods_data->begin_day,
                'duration' => $goods_data->duration,
                'direct_push' => $goods_data->direct_push,
                'second_push' => $goods_data->second_push,
                'service_rate' => $goods_data->service_rate,
                'lock_rate' => $goods_data->lock_rate,
                'type' => 2,
                'pledge_status' => 0,
                'start_time' => date("Y-m-d",strtotime('+1 day')),
                'end_time' => date("Y-m-d",strtotime('+'.($goods_data->duration).' day'))
            ]);

            if (!empty($value['suocang'])) {
                \app\common\model\User::where('id',$user_id)->setInc('locks',$value['suocang']);
                \app\admin\model\WaterLock::create([
//                    'detail_id' => $order_data->id,
                    'user_id' => $user_id,
                    'type' => 1,
//                'coin_type' => "FIL",
                    'money' =>  $value['suocang'],
                    'balance' => $value['suocang']
                ]);
            }


            \app\common\model\User::where('id',$user_id)->setInc('pledge',$value['pledge']);
            \app\admin\model\WaterPledge::create([
//                    'detail_id' => $order_data->id,
                'user_id' => $user_id,
                'type' => WATER_PLEDGE_PLEDGE,
//                'coin_type' => "FIL",
                'money' =>  $value['pledge'],
                'balance' => $value['pledge']
            ]);
        }
//        $money = 1.166666;
//        var_dump($money);
//        WaterMining::create([
//            'user_id' => 1,
//            'type' => 1,
//            'money' => $money,
//            'coin_type' => '111'
////            'balance' => $balance + $profit
//        ]);
//        MiningUserWallet::where('user_id',1)->setInc('balance',$money);
//        $sms = new \app\common\library\Sms();
//        $msg = '你的短信验证码是：884317';
//        $sms->notice(13072648292,$msg);
        return $this->view->fetch();
    }

}
